﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TvGuide.UI.Models;
using TVGuide.DataAccess.Repository;
using TVGuide.DataAccess.Common;
using TVGuide.Parser;
using TVGuide.Common;

namespace TvGuide.UI.Controllers
{
    public class HomeController : BaseController
    {
        private int lang;
        
        [HttpGet]
        public ActionResult Home(string lng, int? ch, int? id)
        {
            this.lang = Utils.LanguageSelect(lng);
            ViewData["lng"] = lng;
            ViewData["Runing"] = false;
            ViewData["h1content"] = "Armenia TV Online";
            if (ch == null)
            {
                return NowRuning(lng, ch, id);
            }
            else
                return View();          
        }

        [HttpGet]
        public ActionResult ShareSocial(string lng, int? ch, int? id)
        {
            int? lineindex = null;
            if (id > 10)
                lineindex = ProgramData.ParseParam(ref id);

            ViewData["selch"] = ch;
            ViewData["selday"] = id;
            ViewData["lng"] = lng;
            this.lang = Utils.LanguageSelect(lng);
            ProgramData PrData = new ProgramData(this.lang);

            ViewData["armlink"] = "/am/" + ch.ToString() + "/" + id.ToString();
            ViewData["englink"] = "/en/" + ch.ToString() + "/" + id.ToString();

            PrData.MakeingWeekDays();
           // PrData.ChannelsList();
            PrData.ProgramsList(ProgramData.GetPrDate(ref id), (int)ch, lineindex);
            PrData.ChID = (int)ch;
            PrData.WeekDayID = (int)id;

            return View("Home",PrData);
        }
        public ActionResult NowRuning(string lng, int? ch, int? id)
        {
            ViewData["Runing"] = true;
            ViewData["lng"] = lng;
            ViewData["title"] = "Armenia TV Online - Online Armenia TV Portal | TVCuyc.am";
            ProgramData PrData = new ProgramData(this.lang);
           
            PrData.MakeingWeekDays();
           // PrData.ChannelsList();
            PrData.NowRuning(DateTime.Now.AddHours(Utils.ZoneHour),lng);
            ViewData["selch"] = "H1-tv/"+10;
            ViewData["selday"] = 1;
            
            PrData.ChID = 10;
            PrData.WeekDayID = 1;
            ViewData["armlink"] = "/am";
            ViewData["englink"] = "/en"; 
           // SearchData.Catlist = PrData.Catlist;
            return View(PrData);
        }

        public JsonResult GetRuning(string lng, int? ch, int? id)
        {
            ViewData["Runing"] = true;
            ViewData["lng"] = lng;
            ViewData["displayVal"] = "block";
            this.lang = Utils.LanguageSelect(lng);
            
            ProgramData PrData = new ProgramData(this.lang);
            PrData.NowRuning(ProgramData.SetOnAirDate(ch), lng);
            PrData.WeekDayID = (int)id;
            return Json(PrData);
        }

        [HttpGet]
        public JsonResult GetMostViewed()
        {

            var viewCount = new Dictionary<int, int>();

            foreach (var sessionInfo in OnlineTVController.sessionInfo)
            {
                if (viewCount.ContainsKey(sessionInfo.ChannelId))
                {
                    viewCount[sessionInfo.ChannelId]++;
                }
                else
                {
                    viewCount.Add(sessionInfo.ChannelId, 1);
                }
            }

            List<KeyValuePair<int, int>> myList = viewCount.ToList();

            myList.Sort((firstPair, nextPair) =>
            {
                return -firstPair.Value.CompareTo(nextPair.Value);
            }
            );


            var popularPrograms = new List<ProgramViewModel>();

            for (var i = 0; i < Math.Min(5, myList.Count); i++)
            {
                var prData = new ProgramData(1);
                prData.NowRuning(DateTime.Now.AddHours(Utils.ZoneHour), "arm");
                var currentPrograms = prData.NowRunnList.Where(x => x.chID == myList[i].Key && x.Date <= DateTime.Now.AddHours(Utils.ZoneHour));
                var currentProgram = currentPrograms.LastOrDefault();

                var progViewModel = new ProgramViewModel();
                progViewModel.ChannelDisplayName = currentProgram.ChdispName;
                progViewModel.ChannelId = currentProgram.chID;
                progViewModel.ChannelLogoURL = currentProgram.ChimgUrl;
                progViewModel.ProgramName = currentProgram.PrName;

                popularPrograms.Add(progViewModel);

            }


            return Json(popularPrograms, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetProg(string lng, string ch_name, int? ch, int? id)
        {
            var url = Request.Url;
            ViewData["og_url"] = url.AbsoluteUri;
            ViewData["Runing"] = false;
            ViewData["lng"] = lng;
            ViewData["displayVal"] = "none";
            this.lang = Utils.LanguageSelect(lng);
            ViewData["selch"] = ch_name+"/"+ch;
            ViewData["selday"] = id;
            ViewData["title"] = ch_name.Split(new string[] { "-tv" }, StringSplitOptions.None)[0] + " TV schedule - Armenia TV Online " + "| TVCuyc.am";

            ViewData["chanalname"] = ch_name.Split(new string[] { "-tv" }, StringSplitOptions.None)[0] + " TV";

            ViewData["h1content"] = ch_name.Split(new string[] { "-tv" }, StringSplitOptions.None)[0] + " TV";
           
            ProgramData PrData = new ProgramData(this.lang);
            PrData.MakeingWeekDays();
           // PrData.ChannelsList();
            PrData.ProgramsList(ProgramData.GetPrDate(ref id), (int)ch,null);
            PrData.ChID = (int)ch;
            PrData.WeekDayID = (int)id;
            ViewData["og_img"] = "http://"+url.Host + PrData.ChFbImg;            
            
            ViewData["armlink"] = "/am/" + "schedule/"+ch_name+"/"+ ch.ToString() + "/" + id.ToString();
            ViewData["englink"] = "/en/" +"schedule/"+ch_name+"/"+ ch.ToString() + "/" + id.ToString();
           
            return View("Home", PrData);
        }


        public ActionResult Football(string lng)
        {
            this.lang = Utils.LanguageSelect(lng);
            ProgramData PrData = new ProgramData(this.lang);
            return View(PrData);
        }

        public ActionResult Do()
        {
            return View();
        }

        public ActionResult DoUcom()
        {
            ProgramRepository pr = ProgramRepository.Instance();
            MainProgram.ParseNewUcomFromFile(Server.MapPath("/dev/==SecureAppDataSecure==/newUcomProgramHTMLPlace/uTVEpg.html"));
            return RedirectToAction("Do");
        }

        public ActionResult DoNtv()
        {
            ProgramRepository pr = ProgramRepository.Instance();
            MainProgram.NTVParseForWeek();
            return RedirectToAction("Do");
        }
        
        public ActionResult DoTorTv()
        {
            MainProgram.ParseTorrentTV();
            return RedirectToAction("Do");
        }
        

        public ActionResult DoEter()
        {
            ProgramRepository pr = ProgramRepository.Instance();
            MainProgram.EterParseForWeek();
            return RedirectToAction("Do");
        }

        public ActionResult DoOther()
        {
            ProgramRepository pr = ProgramRepository.Instance();
            MainProgram.ParseForWeek();
            return RedirectToAction("Do");
        }

        public ActionResult MoveCatUpdates()
        {
            TVGuide.DataAccess.Utilsities.moveCatProgsToPrograms();             
            return View();
        }
    }
}
